package ServiceImpl;

import java.util.List;

import Bean.User;
import Dao.UserDao;
import Service.UserService;

public class UserServiceImpl implements UserService {

	@Override
	public User login(String username, String password) {
		UserDao userDao = new UserDao();
		User user = null;
		try {
			user = userDao.selectByUsernameAndPassword(username, password);
		} catch(Exception e) {
			return null;
		}
		if(user != null) {
			return user;
		}
		return null;
	}

	@Override
	public boolean adduser(User user) {
		UserDao userDao = new UserDao();
		User tmp = null;
		try {
			tmp = userDao.selectByUsername(user.getUsername());
			if(tmp == null) {
				System.out.println("before");
				userDao.insert(user);
				System.out.println("after");
				return true;
			} else {
				return false;
			}
			// tmp = userDao.selectByUsername(user.getUsername());
		} catch(Exception e) {
			System.out.println("exception");
			return false;
		}
	}

	@Override
	public List<User> selectAllUser() {
		UserDao userDao = new UserDao();
		try {
			return userDao.selectAllUser();
		} catch(Exception e) {
			return null;
		}
	}

	@Override
	public User selectUser(String username) {
		User user = null;
		UserDao userDao = new UserDao();
		try {
			user = userDao.selectByUsername(username);
			return user;
		} catch(Exception e) {
			return null;
		}
	}

	@Override
	public boolean deleteUser(String username) {
		UserDao userDao = new UserDao();
		try {
			userDao.dropByUsername(username);
			return true;
		} catch(Exception e) {
			return false;
		}
	}

	@Override
	public boolean updateUserPassword(String username, String password, String newPassword) {
		UserDao userDao = new UserDao();
		try {
			userDao.updatePassword(username, password, newPassword);
			return true;
		} catch(Exception e) {
			System.out.println("exception");
			return false;
		}
	}
	
	
}
